# -*- coding: utf-8 -*-
# @Time : 2022/3/24 17:00

from Common.mysql import db
from Common.public_method import utc_8
from src.ApiTest.ProjectConfig.Database.sprint_database import Sprint
from Common.yaml_method import YamlMethod


class AddSprint:
    """
    新增项目迭代信息
    """
    @staticmethod
    def add_sprint(sprint_name, project_id, project_name, create_user):
        """
        新增项目
        :param sprint_name: 迭代名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param create_user: 创建人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        sprint = Sprint.query.filter_by(projectId=project_id, sprintName=sprint_name).first()
        if sprint is None:
            create_time = utc_8()
            # 项目信息插入数据库
            sprint = Sprint(sprintName=sprint_name, projectId=project_id, projectName=project_name,
                            create_time=create_time, create_user=create_user)
            db.session.add(sprint)
            db.session.commit()

            res = {
                'code': code[0],
                'message': 'success',
                'data': {
                    'sprintName': sprint_name
                }
            }
            return res
        else:
            res = {
                'code': code[1],
                'message': '迭代名已存在',
                'data': {}
            }
            return res
